package com.glory.wxelectricitypda.activity;


import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.FTTestSetUpQueryAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.FTTestSetUpQueryBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;

public class FTTestSetUpActivity extends BaseDialogActivity {


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.eqpID)
    FreeUI_ClearEditText eqpID;
    @BindView(R.id.testeqpId)
    FreeUI_ClearEditText testeqpId;
    @BindView(R.id.lotID)
    FreeUI_ClearEditText lotID;
    @BindView(R.id.lotQty)
    TextView lotQty;
    @BindView(R.id.lotStatus)
    TextView lotStatus;
    @BindView(R.id.orderID)
    TextView orderID;
    @BindView(R.id.materlalName)
    TextView materlalName;
    @BindView(R.id.pocess)
    TextView pocess;
    @BindView(R.id.eqpStatus)
    TextView eqpStatus;
    @BindView(R.id.testEqpID)
    TextView testEqpID;
    @BindView(R.id.prgName)
    TextView prgName;
    @BindView(R.id.testEqpStatus)
    TextView testEqpStatus;
    @BindView(R.id.testProgressRecycler)
    RecyclerView testProgressRecycler;

    private FTTestSetUpQueryAdapter mFTTestSetUpQueryAdapter;
    private List<String> pdaMasterIDList = new ArrayList<>();
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fttest_set_up;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("FT测试设置");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (lotID.getText().toString().equals("")) {
                    showToast("请扫描批次ID", false);
                    return;
                }
                if (eqpID.getText().toString().equals("")) {
                    showToast("请扫描分选机ID", false);
                    return;
                }

                if (testeqpId.getText().toString().equals("")) {
                    showToast("请扫描测试机ID", false);
                    return;
                }
                submit();
            }
        });

        testProgressRecycler.setLayoutManager(new LinearLayoutManager(FTTestSetUpActivity.this));
        testProgressRecycler.setItemAnimator(new DefaultItemAnimator());
        testProgressRecycler.addItemDecoration(new DividerItemDecoration(FTTestSetUpActivity.this,1));
        mFTTestSetUpQueryAdapter = new FTTestSetUpQueryAdapter(null);
        mFTTestSetUpQueryAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mFTTestSetUpQueryAdapter.isFirstOnly(true);
        testProgressRecycler.setAdapter(mFTTestSetUpQueryAdapter);

    }

    @Override
    protected void initData() {

        initEdit(lotID,s->{

            if (eqpID.getText().toString().equals("")) {
                showToast("请扫描分选机ID", false);
                return;
            }

            if (testeqpId.getText().toString().equals("")) {
                showToast("请扫描测试机ID", false);
                return;
            }
            queryTestSelect();
        });

    }

    private void submit(){

        int position = mFTTestSetUpQueryAdapter.getPosition();
        if (position == -1){
            showToast("请选择测试进度", false);
            return;
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID.getText().toString());
            jsonObject.put("TesteqpId", testeqpId.getText().toString());
            jsonObject.put("LotID", lotID.getText().toString());
            jsonObject.put("PdaMasterID", pdaMasterIDList.get(position));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("startTest","StartTest",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<Object>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("提交成功",true);
                }
            }

            @Override
            public void onError(Response<ResponseBean<Object>> response) {
                super.onError(response);
                loadError(response.getException(), "startTest");
            }
        });

    }


    private void queryTestSelect(){

        mFTTestSetUpQueryAdapter.setNewData(null);

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID.getText().toString());
            jsonObject.put("TesteqpId", testeqpId.getText().toString());
            jsonObject.put("LotID", lotID.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("startTestSelect","startTestSelect",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<FTTestSetUpQueryBean>>(
                new TypeToken<ResponseBean<FTTestSetUpQueryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<FTTestSetUpQueryBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    lotQty.setText(response.body().getBody().getLotQty());
                    lotStatus.setText(response.body().getBody().getLotStatus());
                    orderID.setText(response.body().getBody().getOrderID());
                    materlalName.setText(response.body().getBody().getMaterIalname());
                    pocess.setText(response.body().getBody().getPocess());
                    eqpStatus.setText(response.body().getBody().getEqpStatus());
                    testEqpID.setText(response.body().getBody().getTestEqpid());
                    prgName.setText(response.body().getBody().getPrgName());
                    testEqpStatus.setText(response.body().getBody().getTestEqpStatus());
                    List<FTTestSetUpQueryBean.TestProgDTO> testProg = response.body().getBody().getTestProg();
                    Collections.reverse(testProg);
                    mFTTestSetUpQueryAdapter.setNewData(testProg);
                    for (FTTestSetUpQueryBean.TestProgDTO bean:response.body().getBody().getTestProg()) {
                        pdaMasterIDList.add(bean.getPdaMasterID());
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<FTTestSetUpQueryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "startTestSelect");
            }
        });


    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }

}